package sunshine.commcon.util.util;


import com.alibaba.druid.filter.config.ConfigTools;

import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;

/**
 * Database username and password encryption utility
 */
public class DruidEncryputUtil {

    private static String publicKey;


    private static String privateKey;


    static {
        try {
            String[] keyPair = ConfigTools.genKeyPair(512);
            privateKey = keyPair[0];
            System.out.println("privateKey:" + privateKey);
            publicKey = keyPair[1];
            System.out.println("publicKey:" + publicKey);

        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException(e);
        } catch (NoSuchProviderException e) {
            throw new RuntimeException(e);
        }
    }

    public static String encrypt(String password) throws Exception {
        // Use private key to encrypt
        String encrypt = ConfigTools.encrypt(privateKey, password);
        System.out.println("Encrypted content: " + encrypt);
        return encrypt;

    }

    public static String decrypt(String password) throws Exception {
        // Use public key to decrypt
        String decrypt = ConfigTools.decrypt(publicKey, password);
        System.out.println("Decrypted content: " + decrypt);
        return decrypt;

    }

    /**
     * Get the public key for configuration file
     * @return public key
     */
    public static String getPublicKey() {
        return publicKey;
    }

    public static void main(String[] args) {
        try {
            String password = "sunshine8023";
            String encrypted = encrypt(password);
            System.out.println("Encrypted content: " + encrypted);
            String decrypted = decrypt(encrypted);
            System.out.println("Decrypted content: " + decrypted);
            System.out.println("Public key (for configuration file): " + publicKey);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

/*    static {
        try {
            String[] strings = ConfigTools.genKeyPair(512);
            privateKey = strings[0];
            System.out.println("privateKey:"+ privateKey);
            publicKey = strings[1];
            System.out.println("publicKey:"+ publicKey);
        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException(e);
        } catch (NoSuchProviderException e) {
            throw new RuntimeException(e);
        }
    }

    public static String encrypt(String username) {
        try {
            // 使用私钥加密
            return ConfigTools.encrypt(privateKey, username);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
    
    public static String decrypt(String password) {
        try {
            // 使用公钥解密
            return ConfigTools.decrypt(publicKey, password);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }


    public static void main(String[] args) {
        try {
            String root = encrypt("sunshine8023");
            System.out.println("加密后的内容: " + root);
            String decrypted = decrypt(root);
            System.out.println("解密后的内容: " + decrypted);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }*/


